package Game;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import GUIInterfaces.OregonPanel;
import GUIInterfaces.OregonWindow;


public class HuntGamePanel extends JPanel implements OregonPanel,
		MouseListener, KeyListener, Runnable, MouseMotionListener {

	
	BufferedImage images[] =new BufferedImage[5];
	int background = 0; int hunter = 1; int armadillo = 2; int coyote = 3;
	int vulture = 4;
	
	GameDriver gd;
	ArrayList<Rectangle> animals = new ArrayList<Rectangle>();
	ArrayList<Integer> dirs = new ArrayList<Integer>();
	ArrayList<Rectangle> bullets = new ArrayList<Rectangle>();
	double playerx = 300; double playery = 524;
	double rotation;
	volatile boolean up,down,left,right, shoot;
	
	Thread play;
	boolean running = true;
	
	OregonWindow parent;
	private String property;
	
	public HuntGamePanel (OregonWindow parent,GameDriver gd) {
		setGameDriver(gd);
		this.setFocusable(true);
		this.parent = parent;
		String folder = System.getProperty("user.dir")+ File.separator + "images" + File.separator;
		try {
			images[background] = ImageIO.read(new File(folder+"huntmap.png"));
			images[hunter] = ImageIO.read(new File(folder+"hunter.png"));
			images[armadillo] = ImageIO.read(new File(folder+"armadillo.png"));
			images[coyote] = ImageIO.read(new File(folder+"coyote.png"));
			images[vulture] = ImageIO.read(new File(folder+"vulture.png"));
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Could not load hunt images.");
			
		}
		initialise();
	}
	public void initialise() {
		animals.clear();
		bullets.clear();
		for (int i = 0; i < 10; i++) {
			int x = (int)(Math.random() * 500) + 100;
			int y = (int)(Math.random()* 300) + 20;
			int dir = (int)(Math.random()* 3);
			animals.add(new Rectangle(x,y,82,26));
			dirs.add(5*(dir-1));
		}
		playerx = 300;
		playery = 520;
	}
	public void start() {
		addKeyListener(this);
		this.requestFocusInWindow();
		play = new Thread(this);
		play.start();
		initialise();
		running = true;
	}
	public void stop() {
		running = false;
		
	}
	@Override
	public void run() {
		int reload = 3;
		int bulletCount = 3;		
		// TODO Auto-generated method stub
		while (running) {
			try {
				Thread.sleep(20);
			} catch(Exception e) {
				
			}
			int step = 5;
			
			synchronized(this) {
				if (left) 
					playerx -= step;
				if (right) 
					playerx += step;
				if (shoot) {
					if (reload <= bulletCount) {
						bullets.add(new Rectangle((int)playerx+46,(int)playery,10,10));
						bulletCount = 0;
					}  
						
				}
				bulletCount++;
			}
			moveAnimals();
			bulletUpdate();
			detectContact();
			update(this.getGraphics());
		}
	}
	public void detectContact() {
		int i =0, j= 0;
		boolean contact = false;
		while (i < bullets.size()) {
			contact = false;
			while(j < animals.size()) {
				if (animals.get(j).intersects((bullets.get(i)))) {
					contact = true;
					bullets.remove(i);
					animals.remove(j);
				} else {
					j++;
				}
					
				
			}
			if (!contact)
				i++;
		}
	}
	public void moveAnimals() {
		int i = 0;
		while (i < animals.size()) {
			Rectangle rect = animals.get(i);
			
			rect.x+=dirs.get(i);
			if (rect.x < 0) {
				dirs.set(i, 5);
			} else if (rect.x > 770) {
				dirs.set(i, -5);
			} 
			animals.set(i, rect);
			i++;
		}
	}
	public void bulletUpdate() {
		int i = 0;
		while (i < bullets.size()) {
			Rectangle rect = bullets.get(i);
			
			rect.y-=10;
			if (rect.y < 0) {
				bullets.remove(i);
			} else {
 				bullets.set(i, rect);
				i++;
 				
			}
		}
	}
	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		synchronized(this) {
			if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
				left = true;
			} else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
				right =true;
			} else if (arg0.getKeyCode() == KeyEvent.VK_SPACE) {
				shoot = true;
			} else if (arg0.getKeyCode() == KeyEvent.VK_ESCAPE) {
				property = (10-animals.size()) + "";
				parent.done(this);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
			left = false;
		} else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
			right =false;
		} else if (arg0.getKeyCode() == KeyEvent.VK_SPACE) {
			shoot = false;
		}
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("hudsf");
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public GameDriver getGameDriver() {
		// TODO Auto-generated method stub
		return gd;
	}

	@Override
	public void setGameDriver(GameDriver gd) {
		// TODO Auto-generated method stub
		this.gd = gd;
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getProperty() {
		// TODO Auto-generated method stub
		return property;
	}
	public void update(Graphics g) {
		Graphics2D g2d= (Graphics2D)g;
		g2d.drawImage(images[background], 0, 0, null);
		g2d.drawImage(images[hunter], (int)playerx,(int)playery, 92, 131, this);
		for (Rectangle rect : bullets) {
			g2d.fillOval(rect.x, rect.y, rect.width, rect.height);
		} 
		for (Rectangle rect : animals) {
			g2d.drawImage(images[armadillo], rect.x,rect.y, rect.width, rect.height, this);
		}
	}
}
